//
//  NetworkManager.m
//  
//
//  Created by MT on 2017/11/18.
//  Copyright © 2017年 MT. All rights reserved.
//

#import "NetworkManager.h"

@implementation NetworkManager

+ (void)get:(NSString *)url parameters:(id)params success:(void (^)(id object))success failure:(void (^)(NSError *error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)post:(NSString *)url parameters:(id)params success:(void (^)(id object))success failure:(void (^)(NSError *error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];
    
    manager.requestSerializer.timeoutInterval = 15.f;
    
    [manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            if ([[NSString stringWithFormat:@"%@",responseObject[@"status"]] isEqualToString:@"-1"]) {
//                status -1 登录失效,
                PushNotice(ReLogin, nil);
                return;
            }
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end
